$(function() {
	$('#timeCondition').combobox({
		onSelect : function(value) {
			if (value.value == "define") {
				$("#timeFromTo").show();
			} else {
				$("#timeFromTo").hide();
			}
		}
	});
	$('#timeCondition').combobox('setValue', "");
	
	queryTime();
});

function queryTime() {
	var timeType = $('#timeCondition').combobox('getValue');
	var startTime = getDatetimeBoxMillSecond("startTime_query");
	var endTime = getDatetimeBoxMillSecond("endTime_query");
//	if (type == 'define' && startTime == 0 && endTime == 0) {
//		$.messager.alert('系统提示', '请先选择查询条件!');
//		return;
//	}
	$('#dg').datagrid({
		url : getHttpRequestPath("/statistics/queryTimeStatistics"),
		pageNumber : 1,
		queryParams : {
			type : timeType,
			startTime : startTime,
			endTime : endTime
		}
	});
	
	queryGraph(timeType, startTime, endTime);
}

function queryGraph(timeType, startTime, endTime) {
	var dates = [];
	var orders = [];
	$.post(getHttpRequestPath("/statistics/queryTimeGraph"), {
		type : timeType,
		startTime : startTime,
		endTime : endTime
	}, function(typesString) {
		JSON.parse(typesString, function(key, value) {
			if (key == 'name') {
				dates.push(value);
			}
			if (key == 'value') {
				orders.push(value);
			}
		});
		createGraph(dates, orders);
	}).error(_error);
}

function createGraph(dates, orders) {
	$('#container').highcharts({
		chart: {
			type: 'column'
		},
		title: {
			text: '时间柱形图表'
		},
		xAxis: {
			categories: dates,
		},
		yAxis: {
			min: 0,
			title: {
				text: '订单数'
			}
		},
		series: [{
			name: '订单数',
			data: orders
		}]
	});
}

function formatTime(val, row, index) {
	var str = "";
	str += row.name;
	return str;
}

function formatOrders(val, row, index) {
	var str = "";
	str += row.value;
	return str;
}